package ch.fhnw.qualiscan.core.helpers;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Hash {

	public Hash() {}
	
	/**
	 * Generates a hash for a SurveyUserRole object
	 * @param userId id of the user 
	 * @param surveyId id of the survey
	 * @param roleId id of the role
	 * @return the hash code
	 */
	public String generateSurHash(int surveyId, int userId, int roleId) { 
		
		String encryptThis = "amen" + userId + surveyId + roleId;
		MessageDigest mdEnc = null;
		
		try {
			mdEnc = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		} 
		
		// Encryption algorithm
		mdEnc.update(encryptThis.getBytes(), 0, encryptThis.length());
		String md5 = new BigInteger(1, mdEnc.digest()).toString(16); // Encrypted string
		
		return md5;
	}
	
	public String encryptPassword(String password) {
		String encryptThis = "pwd" + password;
		MessageDigest mdEnc = null;
		
		try {
			mdEnc = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		} 
		
		// Encryption algorithm
		mdEnc.update(encryptThis.getBytes(), 0, encryptThis.length());
		String md5 = new BigInteger(1, mdEnc.digest()).toString(16); // Encrypted string
		
		return md5;

	}
	
}
